<?php
require_once 'class/GetGPW.php';
require_once 'class/GetYahoo.php';
require_once 'class/AbstractData.php';
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of AbstractDataIN
 *
 * @author mateusz.ledzewicz
 */
abstract class AbstractDataIN extends AbstractData {
    //put your code here
    
    const GPW   = 0;
    const Yahoo = 1;
    public $numberOfData = 50;
    
    protected $get;
    protected $isin;
    protected $date;
    
    protected $moneyInternet;
    public function __construct($isin, $date, $type, $numberOfData, $currency = null) 
    {    
        $this->numberOfData = $numberOfData;
        $this->isin = $isin;
        $this->date = $date;
        
        
        $config = $this ->getConfig();
        
        
        if($currency)
        {
            $config2 = $config;
            $config2['isin'] = $currency;
            $money = new GetMoney($config2,';');
            $this->moneyInternet = $money->getArray();  
        }
        
        switch($type){
            case self::GPW:
                $this->get = new GetGPW($config);
                break;
            case self::Yahoo:
                $this->get = new GetYahoo($config);
                break;
            default:
                throw new Exception('Type not found');
                break;
        }
        $this->get->removeHeader();
        $data = $this->get->getArray();
        while(count($data) > $this->numberOfData)
        {
            array_pop($data);
        }
        $this->dataInternet = $data;
       
       
        
        $this->prepareData();
        
        
    }
    
    public function AddMoney($type)
    {
        
        $className = get_class($this->get);
        $dateIndex = $className::$mapper[$className::date];
        $dateTo = array_values($this->dataInternet)[0][$dateIndex];
        $dateFrom = end($this->dataInternet)[$dateIndex];
       
        
      
        $config = array(
            'dateFrom' => $dateFrom,
            'dateTo' => $dateTo,
            'instrumentType' => '10',
            'isin' => $type
        );
        $get = new GetMoney($config,';');
        $moneyArr = $get->getArray();
        
        $array_slice = array_slice($this->dataInternet, 0, count($this->data));
       
        //musi być mapowane po dacie
        $i = 0;
        foreach($array_slice as $ar )
        {
           $this->data[$i][]=  floatval($moneyArr[$ar[$dateIndex]]);
           $i++;
        }
        
        
    }
    
    protected function getConfig()
    {
        $dateTo     = $this->date;
        $date       = new DateTime($dateTo);
        $days = ceil($this->numberOfData + (ceil($this->numberOfData/7)*2)*2+4);
        $date       = $date->sub(new DateInterval('P'.$days.'D'));
        $dateFrom   = $date->format('Y-m-d'); 
        
        return array(
            'dateFrom' => $dateFrom,
            'dateTo' => $dateTo,
            'instrumentType' => '10',
            'isin' => $this->isin
        );
    }
    
    public function getPreparedData()
    {
        return $this->dataInternet; 
    }
    
    
   
    
   
    
 
}

?>
